package com.smwish;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class SignWishlistServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(SignWishlistServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String content = req.getParameter("wish").trim();
        log.warning("Reieved wish:" + content);
        if( content != null && content.length() != 0 ){
	        Date date = new Date();
	        Wish greeting = new Wish(user, content, date);
	
	        PersistenceManager pm = PMF.get().getPersistenceManager();
	        try {
	            pm.makePersistent(greeting);
	        } finally {
	            pm.close();
	        }
	
	        resp.sendRedirect("/wishlist.jsp?m=1");
        }
    }
}